package com.at.abc.r419_e;

import java.io.*;
import java.util.Arrays;

public class e {

    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StreamTokenizer in = new StreamTokenizer(br);
    static PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) throws IOException {
        solve();
        out.flush();
        out.close();
        br.close();
    }

    static int MAXN = 505;
    static int n, m, l;
    static int[] a = new int[MAXN];
    static int[][] dp = new int[MAXN][MAXN];
    static int[][] g = new int[MAXN][MAXN];

    static void solve() throws IOException {
        in.nextToken(); n = (int) in.nval;
        in.nextToken(); m = (int) in.nval;
        in.nextToken(); l = (int) in.nval;
        for (int i = 1; i <= n; i++) {
            in.nextToken(); a[i] = (int) in.nval;
        }
        for (int i = 1; i <= l; i++) {
            for (int j = 0; j < m; j++) {
                g[i][j] = 0;
            }
            for (int p = i; p <= n; p += l) {
                for (int j = 0; j < m; j++) {
                    g[i][j] += (j - a[p] + m) % m;
                }
            }
        }
        final int INF = 1_000_000_000;
        for (int i = 0; i <= l; i++) {
            Arrays.fill(dp[i], INF);
        }
        dp[0][0] = 0;
        for (int i = 1; i <= l; i++) {
            for (int j = 0; j < m; j++) {
                int best = INF;
                for (int k = 0; k < m; k++) {
                    int prev = dp[i - 1][(j - k + m) % m];
                    if (prev == INF) continue;
                    int cand = prev + g[i][k];
                    if (cand < best) best = cand;
                }
                dp[i][j] = best;
            }
        }

        out.println(dp[l][0]);
    }
}
